package com.pp.admin.main.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.pp.admin.common.client.FooterPanel;
import com.pp.admin.common.client.FooterParent;
import com.pp.admin.common.client.PermissionCheck;
import com.pp.admin.common.client.PermissionNode;
import com.pp.admin.common.client.beans.LoginUserInfo;
import com.pp.admin.common.client.event.AfterLoginHandler;
import com.pp.admin.common.client.i18.I18;
import com.pp.admin.common.client.service.Service;
import com.pp.admin.main.client.tab.RootTab;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Administrator implements EntryPoint, I18, FooterParent, Service {

	private VerticalPanel adminRootPanel = null;

	private HeaderPanel header = null;

	private static LoginUserInfo currentUser = new LoginUserInfo(myConstants.guest(), PermissionCheck.APP_DOMAIN);

	private PermissionCheck permCheck = new PermissionCheck(new PermissionNode(PermissionCheck.APP_DOMAIN));

	private List<AfterLoginHandler> afterLoginHandlerlist = new ArrayList<AfterLoginHandler>();

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		loginService.createUser(GWT.getHostPageBaseURL(), new AsyncCallback<Void>() {

			@Override
			public void onSuccess(Void result) {

				// TODO Auto-generated method stub

			}

			@Override
			public void onFailure(Throwable caught) {

				// TODO Auto-generated method stub

			}
		});
		adminRootPanel = new VerticalPanel();
		adminRootPanel.setWidth("100%");
		header = new HeaderPanel(this);
		adminRootPanel.add(header);
		adminRootPanel.setCellHeight(header, "70px");
		RootTab tab = new RootTab(this, 40, Unit.PX);
		addAfterLoginHandler(tab);
		header.addAfterLogoutHandler(tab);
		adminRootPanel.add(tab);
		adminRootPanel.add(new FooterPanel(this));
		// adminRootPanel.addWest(new HTML("navigation"), 10);
		RootLayoutPanel rp = RootLayoutPanel.get();
		rp.add(adminRootPanel);
		adminRootPanel.getElement().getOffsetParent().addClassName("admin-root-panel");
		checkIfUserIsLogedIn();
	}

	private void checkIfUserIsLogedIn() {
		loginService.isUserLogedIn(PermissionCheck.APP_DOMAIN, new AsyncCallback<LoginUserInfo>() {

			@Override
			public void onSuccess(LoginUserInfo result) {

				currentUser = result;
				if (currentUser != null) {
					header.setUserInfo(result);
				} else {
					currentUser = new LoginUserInfo(myConstants.guest(), PermissionCheck.APP_DOMAIN);
				}
				refresh();
			}

			@Override
			public void onFailure(Throwable caught) {

				currentUser = new LoginUserInfo(myConstants.guest(), PermissionCheck.APP_DOMAIN);
				refresh();
			}
		});

	}

	public static LoginUserInfo getCurrentUserInfo() {

		return currentUser;
	}

	public static void setCurrentUserInfo(LoginUserInfo user) {

		currentUser = user;
	}

	public void addAfterLoginHandler(AfterLoginHandler handler) {

		afterLoginHandlerlist.add(handler);
	}

	public boolean checkPermission(String permission) {

		return (currentUser.isSuperuser() ? true : permCheck.check(permission));
	}

	public void refresh() {

		Iterator<AfterLoginHandler> iterHandlers = afterLoginHandlerlist.iterator();
		AfterLoginHandler afterLoginHandler = null;
		while (iterHandlers.hasNext()) {
			afterLoginHandler = iterHandlers.next();
			afterLoginHandler.afterLogin();
		}
	}
}

interface LoginInfoService {

	public String getUserId();

	public String getPassword();
}
